package com.inifitness.common.event.publisher;

import java.util.Map;

import com.inifitness.common.event.event.MessageEvent;
import com.inifitness.common.rabbitmq.EventRabbitConfig;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 事件发布器
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024-01-11 14:34
 */
@Component
@Log4j2
@RequiredArgsConstructor
public class EventPublisher {

  private final RabbitTemplate rabbitTemplate;

  public void publishEvent(String eventCode, Object payload) {
    MessageEvent event = new MessageEvent(eventCode, payload);
    rabbitTemplate.convertAndSend(EventRabbitConfig.EVENT_EXCHANGE, EventRabbitConfig.EVENT_ROUTING,
        event);
    log.info("推送事件到RabbitMQ,推送方式:主动, 消息:{}", event);
  }

  public void publishEvent(String eventCode, Map<String, Object> map) {
    MessageEvent event = new MessageEvent(eventCode, map);
    rabbitTemplate.convertAndSend(EventRabbitConfig.EVENT_EXCHANGE, EventRabbitConfig.EVENT_ROUTING,
        event);
    log.info("推送事件到RabbitMQ,推送方式:主动, 消息:{}", event);
  }
}
